<?php
	
	use Workerman\Worker;
	use Workerman\Mysql;
	use \Workerman\Lib\Timer;
	use \Workerman\Protocols\Http;
	use \Workerman\WebServer;
	
	require_once IA_ROOT . "/../Libs/class/debug.class.php";
	require_once IA_ROOT . "/../Libs/class/token.class.php";
    require_once IA_ROOT . "/../Libs/class/PHPMailer/PHPMailer.php";	
    require_once IA_ROOT . "/../Libs/class/PHPMailer/SMTP.php";
    require_once IA_ROOT . "/../Libs/class/PHPMailer/Exception.php";	
	
	$check_overtime_worker = new Worker();
	
	// 启动4个进程对外提供服务
	$check_overtime_worker->count = 1;
	// 每5秒执行一次device状态更新，如果没有更新，则认为离线
	$tm_device_chker_interval = 5;
	//服务启动
	$check_overtime_worker->onWorkerStart = function($worker) use($tm_device_chker_interval){
		require_once IA_ROOT . "/../Libs/function/comm.func.php";
		Timer::add($tm_device_chker_interval , function()
		{
            $now_date = date("Y-m-d H:i:s");
			$sql = "SELECT a.id as poster_id,b.id FROM hf_mes_poster_management a 
                    INNER JOIN hf_mes_poster_contents b ON a.id=b.poster_id 
                    WHERE b.finish='0' AND a.end_time<='$now_date'";
            $ret = sql_query($sql);
            if(empty($ret)){
                $sql = "SELECT id as poster_id FROM hf_mes_poster_management
                    WHERE end_time<='$now_date'";
                $ret = sql_query($sql);
            }
            foreach($ret as $values){
                $poster_contents_id = '';
                $poster_id = $values["poster_id"];
                if(!empty($values["id"])){
                    $poster_contents_id = $values["id"];
                }
                // 把poster_management表中的status置为overtime
                $sql = "UPDATE hf_mes_poster_management SET `status`='overtime' WHERE id='$poster_id'";
                sql_query($sql);

                if(!empty($poster_contents_id)){
                    // 把poster_contents表中的finish置为1
                    $sql = "UPDATE hf_mes_poster_contents SET finish='1' WHERE id='$poster_contents_id'";
                    sql_query($sql);

                    // 把picture_department表中的finish置为1，user_id、deputy_director、chief_inspector如果不存在则置为-2（系统超时锁定）
                    $sql = "SELECT b.id,b.user_id,b.deputy_director,b.chief_inspector FROM hf_mes_poster_contents a
                        INNER JOIN hf_mes_poster_picture_department b ON a.id=b.picture_id
                        WHERE a.id='$poster_contents_id'";
                    $dp = sql_query($sql);
                    foreach($dp as $val){
                        $sql_str = "";
                        $picture_department_id = $val["id"];
                        $user_id = $val["user_id"];
                        $deputy_director = $val["deputy_director"];
                        $chief_inspector = $val["chief_inspector"];
                        if($user_id == ""){
                            $sql_str.= "user_id='-2',";
                        }
                        if($deputy_director == "-1"){
                            $sql_str.= "deputy_director='-2',";
                        }
                        if($chief_inspector == "-1"){
                            $sql_str.= "chief_inspector='-2',";
                        }
                        $sql_str = rtrim($sql_str,",");

                        if($sql_str != ""){
                            $sql = "UPDATE hf_mes_poster_picture_department SET $sql_str WHERE id='$picture_department_id'";
                            sql_query($sql);
                        }

                    }
                }

                // 通知设计师
                $sql = "SELECT a.email FROM hf_mes_user a 
						INNER JOIN hf_mes_groups b ON a.groups=b.id  
                        WhERE  b.class='设计师'";
                $email_arr = [];
                $notice = sql_query($sql);
                foreach($notice as $val){
                    array_push($email_arr,$val["email"]);
                }
                $flag = sendMail($email_arr, '有稿件待校', '请用手机登录校稿系统，进行校稿,网址:http://www.sctmes.com:6060/mobile_web/login.php');
                if ($flag) {
                    $obj = "done";
                } else {
                    $obj = "邮件发送失败！";
                }

            }
            
			return;			
		});
	};
	
	
	//关闭服务/udp无效
	$check_overtime_worker->onWorkerStop = function($worker){

	};
	
	
	//客户端链接/udp无效
	$check_overtime_worker->onConnect = function($connection){
		//echo "new connection from ip " . $connection->getRemoteIp() . "\n";
	};
	
	
	//客户端断开链接
	$check_overtime_worker->onClose = function($connection){
		//echo "connection closed\n";
    };
    
	// 如果不是在根目录启动，则运行runAll方法
	if(!defined('GLOBAL_START')){
		Worker::runAll();
	}
	
	
?>

